<template>
  <el-dialog title="分配角色" :visible="isShowRoleDialog" @close="btnCancel">
    <!-- el-checkbox-group选中的是 当前用户所拥有的角色  需要绑定 当前用户拥有的角色-->
    <el-checkbox-group v-model="checkedList">
      <!-- 选项 -->
      <el-checkbox v-for="role in roleList" :key="role.id" :label="role.id">
        {{
          role.name
        }}
      </el-checkbox>
    </el-checkbox-group>
    <template #footer>
      <el-row type="flex" justify="center">
        <el-button type="primary" size="small" @click="btnOK">确定</el-button>
        <el-button size="small" @click="btnCancel">取消</el-button>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
import { getRoleList } from '@/api/setting'
import { assignRoles } from '@/api/employees'
import { getUserDetailByIdapi } from '@/api/user'
export default {
  props: {
    isShowRoleDialog: {
      type: Boolean,
      default: false
    }
    // 用户的id 用来查询当前用户的角色信息
    // userId: {
    //   type: String,
    //   default: null
    // }
  },
  data() {
    return {
      roleList: [],
      checkedList: [],
      userId: ''
    }
  },
  created() {
    this.getRoleList()
  },
  methods: {
    async getRoleList() {
      const { rows } = await getRoleList()
      this.roleList = rows
    },
    async getCheckList(id) {
      this.userId = id
      const { roleIds } = await getUserDetailByIdapi(id)
      this.checkedList = roleIds
    },
    // 封装一个回显当前员工数据的函数，
    async btnOK() {
      await assignRoles({ id: this.userId, roleIds: this.checkedList })
      // 关闭窗体
      this.$message.success('修改成功')
      this.$emit('update:isShowRoleDialog', false)
    },
    async btnCancel() {
      this.checkedList = []
      this.$emit('update:isShowRoleDialog', false)
    }
  }
}
</script>
